package controllayer;

import java.util.ArrayList;

import modellayer.City;
import modellayer.Supplier;
import dblayer.DBConnection;
import dblayer.DBSupplier;
import dblayer.IFDBSupplier;

public class CtrSupplier {

	private IFDBSupplier dbObj;
	private CtrCity ctrCity;
	
	public CtrSupplier(){
		dbObj = new DBSupplier();
		ctrCity = new CtrCity();
	}
	
	public ArrayList<Supplier> getAllSuppliers(){
		ArrayList<Supplier> list = new ArrayList<Supplier>();
		list = dbObj.getAllSuppliers();
		return list;
	}
	
	public Supplier searchByID(int id){
		return dbObj.searchByID(id);
	}
	
	public Supplier searchByName(String name){
		return dbObj.searchByName(name);
	}
	
	public Supplier searchByPhone(int phone){
		return dbObj.searchByPhone(phone);
	}
	
	//
	
	public int updateSupplier(int id, String name, String address, City city, int phone, int cvrNo){
		Supplier supplier = new Supplier(id, name, address, cvrNo, phone, city);
		
		return dbObj.updateSupplier(supplier);
	}
	
	public void createSupplier(String name, String address, int zipCode, String city, int phone, int cvrNo){
		ctrCity.createCity(zipCode, city);
		
		Supplier supplier = new Supplier(name, address, cvrNo, phone, ctrCity.searchByZipCode(zipCode));
		
		try{
			DBConnection.startTransaction();
			dbObj.insertSupplier(supplier);
			DBConnection.commitTransaction();
		}
		catch(Exception e){
			DBConnection.rollbackTransaction();
		}
	}
	
	public int deleteSupplier(int id){
		return dbObj.deleteSupplier(id);
	}
}
